package com.ruifu.bom.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.bom.dto.MaterielAndVersionsDTO;
import com.ruifu.bom.dto.SaveAsDTO;
import com.ruifu.bom.service.MaterielVersionsService;
import com.ruifu.bom.vo.MaterielAndVersionsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物料版本控制层
 *
 * @author nyr
 * @create 2019/12/23
 */
@RestController
@CrossOrigin
@RequestMapping("/materielVersions")
public class MaterielVersionsController {
    @Autowired
    private MaterielVersionsService materielVersionsService;
    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser= SecurityUtils.getUser();
    }

    /**
     * 联合查询
     * @param current
     * @param size
     * @return
     */
    @PostMapping("/findByQuery/{current}/{size}")
    public Page<MaterielAndVersionsVO> findByQuery(@PathVariable long current, @PathVariable long size,
                                                      @RequestBody MaterielAndVersionsDTO materielAndVersionsDTO) {
        try {
            return materielVersionsService.find(new Page<>(current,size),materielAndVersionsDTO,cloudUser);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据版本id查询物料详情
     * @param mvId
     * @return
     */
    @GetMapping("/findByMvId/{mvId}")
    public R findByMvId(@PathVariable String mvId) {
        try {
            return materielVersionsService.findByMvId(mvId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 新建物料(含物料版本)
     *
     * @param materielAndVersionsDTO 物料和物料版本传输对象
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody MaterielAndVersionsDTO materielAndVersionsDTO) {
        try {
            return materielVersionsService.saveMaterielAndVersion(materielAndVersionsDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 修改物料(含物料版本)
     *
     * @param materielAndVersionsDTO 物料和物料版本传输对象
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody MaterielAndVersionsDTO materielAndVersionsDTO) {
        try {
            return materielVersionsService.updateMaterielAndVersion(materielAndVersionsDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 删除物料(含物料版本以及bom结构)
     *
     * @param mvIds 物料版本id集合
     * @return
     */
    @DeleteMapping("/batchDelete")
    public R batchDelete(@RequestBody List<Long> mvIds) {
        try {
            return materielVersionsService.batchDelete(mvIds);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 升级零部件版本
     *
     * @param mvId 物料版本id
     * @return
     */
    @GetMapping("/upVersion/{mvId}")
    public R upVersion(@PathVariable Long mvId) {
        try {
            return materielVersionsService.upVersion(mvId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 查询零部件历史版本
     *
     * @param mvMaterielId 物料id
     * @return
     */
    @GetMapping("/versionHistory/{mvMaterielId}")
    public R versionHistory(@PathVariable Long mvMaterielId) {
        try {
            return materielVersionsService.versionHistory(mvMaterielId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 转交版本所有权
     *
     * @param mvId   版本id
     * @param userId 被转交人
     * @return
     */
    @GetMapping("/transferOwnership/{mvId}/{userId}")
    public R transferOwnership(@PathVariable Long mvId, @PathVariable Long userId) {
        try {
            return materielVersionsService.transferOwnership(mvId,userId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 另存为
     */
    @PostMapping("/saveAs")
    public R saveAs(@RequestBody SaveAsDTO saveAsDTO) {
        try {
            return materielVersionsService.saveAs(saveAsDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }


}
